package com.buka.recipe.system.service.impl;

import cn.hutool.http.Header;
import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import com.buka.recipe.common.utils.AddressUtils;
import com.buka.recipe.common.utils.IpUtils;
import com.buka.recipe.system.entity.LoginLog;
import com.buka.recipe.system.entity.User;
import com.buka.recipe.system.repository.LoginLogRepository;
import com.buka.recipe.system.repository.UserRepository;
import com.buka.recipe.system.service.LoginLogService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

@Log4j2
@Service
public class LoginLogServiceImpl implements LoginLogService {

    private static final String USERNAME_PASSWORD_LOGIN_URI = "/login";
    private static final String SMS_LOGIN_URI = "/login/sms";
    private static final String OAUTH2_LOGIN_URI = "/login/oauth2/code";

    private final LoginLogRepository loginLogRepository;
    private final UserRepository userRepository;

    @Autowired
    public LoginLogServiceImpl(LoginLogRepository loginLogRepository, UserRepository userRepository) {
        this.loginLogRepository = loginLogRepository;
        this.userRepository = userRepository;
    }

    @Override
    public void save(HttpServletRequest request, boolean isSuccess, String message) {
        LoginLog loginLog = new LoginLog();

        // UA 信息
        String userAgentHeader = request.getHeader(Header.USER_AGENT.getValue());
        if (StringUtils.hasLength(userAgentHeader)) {
            UserAgent userAgent = UserAgentUtil.parse(userAgentHeader);
            loginLog.setIsMobile(userAgent.isMobile());
            loginLog.setBrowser(userAgent.getBrowser().getName());
            loginLog.setPlatform(userAgent.getPlatform().getName());
            loginLog.setOs(userAgent.getOs().getName());
        }
        // IP地址、归属地
        String ipAddress = IpUtils.getClientIp(request);
        loginLog.setIp(ipAddress);
        String realAddressByIp = AddressUtils.getRealAddressByIP(ipAddress);
        loginLog.setLocation(realAddressByIp);
        // 登录方式
        String requestUri = request.getRequestURI();
        String username = (String) request.getAttribute("username");
        log.info("login log save user {}", username);
        if (USERNAME_PASSWORD_LOGIN_URI.equals(requestUri)) {
            log.info("账号密码登录");
            User u = userRepository.findByUsername(username);
            loginLog.setAccount(username);
            loginLog.setUserId(u.getId());
        } else if (SMS_LOGIN_URI.equals(requestUri)) {
            log.info("短信验证码登录");
        } else if (OAUTH2_LOGIN_URI.equals(requestUri)) {
            log.info("第三方平台登录");
        }
        loginLog.setTime(new Date());
        loginLog.setMessage(message);
        loginLog.setStatus((short) (isSuccess ? 1 : 0));

        loginLogRepository.save(loginLog);
    }
}
